package com.wwwarehouse.financialcenter.fragment.moneywarehouse.bills;

import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.filter.SeriesBean;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.DrawerSeriesCompleteEvent;
import com.wwwarehouse.common.fragment.DeskDrawerSeriesFragment;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.BillRecordResponseBean;
import com.wwwarehouse.financialcenter.bean.bill.SerializableMap;
import com.wwwarehouse.financialcenter.bean.bindingcard.MoneyHouseInfomationBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.eventbus.Event;
import com.wwwarehouse.financialcenter.eventbus.EventHideTitle;
import com.wwwarehouse.financialcenter.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.common.constant.Constant.sp_User_Id;
import static com.wwwarehouse.financialcenter.constant.FinancialConstant.PATH_BILL_PAYMENT;

/**
 * @author Joseph on 2017/11/14.
 *         <p/>
 *         账单
 */
@Route(path = PATH_BILL_PAYMENT)
public class BillViewPagerFragment extends MyBaseViewPagerFragment {
    private String mBusinessId = "";
    private String mBusinessName = "";
    private int mInOrOut = 0;
    private int mPayStatus = 0;

    private List<FilterBean> mSortList; //排序
    private List<SeriesBean> mFilterList = new ArrayList<>(); //筛选

    private List<FilterBean> mFilterBusinessList = new ArrayList<>(); //业务单位
    private List<FilterBean> mFilterPaymentList = new ArrayList<>(); //支付状态
    private List<FilterBean> mFilterStatusList = new ArrayList<>();  //收支

    private Map mMap;

    private static final int REQUEST_GET_BILL_RECORD = 0;
    private MoneyHouseInfomationBean.DataBean data;  //单个业务单位
    private List<MoneyHouseInfomationBean.DataBean> moneyHomeDataList; //所有的业务单位
    private boolean isFunction  = false; //是否是功能卡
    private boolean isFirst = true;
    @Override
    public void onDestroy() {
        super.onDestroy();

        ((BaseCardDeskActivity) mActivity).showSortBt();
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BillViewPagerFragment)
            if (isFunction){
                mActivity.setTitle(getString(R.string.financial_center_pay));
            }else {
                mActivity.setTitle(getString(R.string.financial_center_billin));
            }

    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
    @Override
    protected void loadDatas() {
       Bundle  bundle = getArguments();
        if (bundle !=null){
            CardDeskFunctionResponseBean.TaskBean taskBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
            if (taskBean!=null) {
                //功能卡
                mPayStatus = 2;
                mInOrOut = 2;
                isFunction = true;
                mBusinessId = taskBean.getBusinessId();
            }

            data = (MoneyHouseInfomationBean.DataBean) bundle.getSerializable("moneyHomeData");
            moneyHomeDataList = (List<MoneyHouseInfomationBean.DataBean>) bundle.getSerializable("moneyHomeDataList");

            if (data!=null){
                mBusinessId = data.getBusinessId() + "";
            }

            if (moneyHomeDataList !=null&& moneyHomeDataList.size()>0){
                    for (int i = 0; i <moneyHomeDataList.size(); i++) {
                        mFilterBusinessList.add(new FilterBean(false, moneyHomeDataList.get(i).getBusinessName(),moneyHomeDataList.get(i)));}
            }
        }

        if (isFunction){
            mActivity.setTitle(getString(R.string.financial_center_pay));
        } else {
            mActivity.setTitle(getString(R.string.financial_center_billin));
        }

//        SeriesBean businessSeriesBean = new SeriesBean();
//        businessSeriesBean.setCount(1);
//        businessSeriesBean.setSingle(true);
//        businessSeriesBean.setTitle("账单所属业务单位");
//        businessSeriesBean.setList(mFilterBusinessList);
//        mFilterList.add(businessSeriesBean);

        //收支
        BillRecordResponseBean.BillRecordDetailsBean inBillRecordDetailsBean = new BillRecordResponseBean.BillRecordDetailsBean();
        inBillRecordDetailsBean.setInOrOut("1");
        FilterBean inFilterBean = new FilterBean(false, "收入", inBillRecordDetailsBean);
        BillRecordResponseBean.BillRecordDetailsBean outBillRecordDetailsBean = new BillRecordResponseBean.BillRecordDetailsBean();
        outBillRecordDetailsBean.setInOrOut("2");
        FilterBean outFilterBean = new FilterBean(false, "支出", outBillRecordDetailsBean);
        mFilterStatusList.add(inFilterBean);
        mFilterStatusList.add(outFilterBean);

        SeriesBean paymentSeriesBean = new SeriesBean();
        paymentSeriesBean.setCount(2);
        paymentSeriesBean.setSingle(false);
        paymentSeriesBean.setTitle("收支");
        paymentSeriesBean.setList(mFilterStatusList);
        mFilterList.add(paymentSeriesBean);

        //支付状态
        BillRecordResponseBean.BillRecordDetailsBean noRecordBillRecordDetailsBean = new BillRecordResponseBean.BillRecordDetailsBean();
        noRecordBillRecordDetailsBean.setPaySatus("1");
        FilterBean noRecordFilterBean = new FilterBean(false, "未出账", noRecordBillRecordDetailsBean);

        BillRecordResponseBean.BillRecordDetailsBean noPayBillRecordDetailsBean = new BillRecordResponseBean.BillRecordDetailsBean();
        noPayBillRecordDetailsBean.setPaySatus("2");
        FilterBean noPayFilterBean = new FilterBean(false,"未付款/未收款", noPayBillRecordDetailsBean);

        BillRecordResponseBean.BillRecordDetailsBean hasPayBillRecordDetailsBean = new BillRecordResponseBean.BillRecordDetailsBean();
        hasPayBillRecordDetailsBean.setPaySatus("4");
        FilterBean hasPayFilterBean = new FilterBean(false,"已付款/已收款", hasPayBillRecordDetailsBean);

        mFilterPaymentList.add(noRecordFilterBean);
        mFilterPaymentList.add(noPayFilterBean);
        mFilterPaymentList.add(hasPayFilterBean);

        SeriesBean statusSeriesBean = new SeriesBean();
        statusSeriesBean.setCount(2);
        statusSeriesBean.setSingle(false);
        statusSeriesBean.setTitle("支付状态");
        statusSeriesBean.setList(mFilterPaymentList);
        mFilterList.add(statusSeriesBean);

        DeskDrawerSeriesFragment deskDrawerMultipleFragment = DeskDrawerSeriesFragment.newInstance(mFilterList);
        ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerMultipleFragment);

        //排序
        mSortList = new ArrayList<>();
        FilterBean filterBean = new FilterBean(false, "支付状态");
        mSortList.add( filterBean);
    }

    public void onEventMainThread(DrawerSeriesCompleteEvent event) {
        if (peekFragment() instanceof BillViewPagerFragment) {
            if (event.isReset()) {
                //重置
                mBusinessName = "";
                mInOrOut = 0;
                mPayStatus = 0;
             ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
            } else {
                List<SeriesBean> seriesList = event.getList();

//                List<FilterBean> businessFilter = seriesList.get(0).getList();
//                List<FilterBean> paymentFilter = seriesList.get(1).getList();
//                List<FilterBean> statusFilter = seriesList.get(2).getList();

                List<FilterBean> paymentFilter = seriesList.get(0).getList();
                List<FilterBean> statusFilter = seriesList.get(1).getList();

//                for (FilterBean filterBean : businessFilter) {
//                    if (filterBean.isSelect()) {
//                        MoneyHouseInfomationBean.DataBean bean  = (MoneyHouseInfomationBean.DataBean) filterBean.getData();
//                        mBusinessId = bean.getBusinessId()+"";
//                        break;
//                    }
//                }

                mInOrOut = 0;

                for (FilterBean filterBean : paymentFilter) {
                    if (filterBean.isSelect()) {
                        BillRecordResponseBean.BillRecordDetailsBean billRecordDetailsBean = (BillRecordResponseBean.BillRecordDetailsBean) filterBean.getData();
                        int inOrStatus = Integer.parseInt(billRecordDetailsBean.getInOrOut());

                        mInOrOut += inOrStatus;
                    }
                }

                mPayStatus = 0;

                for (FilterBean filterBean : statusFilter) {
                    if (filterBean.isSelect()) {
                        BillRecordResponseBean.BillRecordDetailsBean billRecordDetailsBean = (BillRecordResponseBean.BillRecordDetailsBean) filterBean.getData();
                        int payStatus = Integer.parseInt(billRecordDetailsBean.getPaySatus());

                        mPayStatus += payStatus;
                    }
                }
            }
            requestDatas();
        }
    }
    //支付成功之后刷新状态
    @Override
    protected void receiveEvent(Event event) {
        if (event.getCode()== EventHideTitle.CODE.B&&event.getData() instanceof EventHideTitle){
            this.requestDatas();
        }
    }

    @Override
    public void requestDatas() {
        mMap = new HashMap();

        mMap.put("businessId", mBusinessId);
        mMap.put("businessName", mBusinessName);

        if (mInOrOut == 0) {

        } else {
            mMap.put("inOrOut", mInOrOut);
        }

        mMap.put("page", 1);
        mMap.put("size", 1);

        if (mPayStatus == 0) {

        } else {
            mMap.put("paySatus", mPayStatus);
        }

        mMap.put("userId", sp.getValue(sp_User_Id));

        httpPost(FinancialConstant.URL_GET_BILL_RECORD, mMap, REQUEST_GET_BILL_RECORD, false, null);
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what){
            case REQUEST_GET_BILL_RECORD:
                if ("0".equals(commonClass.getCode())) {
                    BillRecordResponseBean billRecordResponseBean = JSON.parseObject(commonClass.getData().toString(), BillRecordResponseBean.class);
                    int total = billRecordResponseBean.getTotal();
                    if (total <= 0){
                        if (isFirst) {
                            isFirst = false;
                            mLoadingView.setVisibility(View.VISIBLE);
                            mLoadingView.showEmptyView(true);
                            mLoadingView.setEmptyListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mLoadingView.showProgressView(true);
                                    isFirst = true;
                                    requestDatas();
                                }
                            });
                        } else {
                            ShowEmptyView();
                        }
                        return;
                    }
                    isFirst = false;
                    Bundle bundle = new Bundle();
                    SerializableMap serializableMap = new SerializableMap();
                    serializableMap.setMap(mMap);
                    bundle.putString("businessId", mBusinessId);
                    bundle.putSerializable("map", serializableMap);
                    bundle.putBoolean("isFunction",isFunction);
                    SetFragmentAndPage(total, 9, new BillChildFragment().getClass().getName(), bundle);
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {
        requestDatas();
    }

    @Override
    public void cancelClick() {
        super.cancelClick();

        if (peekFragment() instanceof BillViewPagerFragment) {
            mBusinessName  = "";

            requestDatas();
        }
    }
    @Override
    public void searchNoRealTimeClick(String text) {
        if (peekFragment() instanceof BillViewPagerFragment) {

            mBusinessName = text;

            requestDatas();
        }
    }
    @Override
    public void filterClick() {
        if (peekFragment() instanceof BillViewPagerFragment) {
            ((BaseCardDeskActivity) mActivity).showDrawerLayout();
        }
    }
    @Override
    public void sortClick(View anchor) {
//        if (peekFragment() instanceof BillViewPagerFragment ){
//            PopUpUtils.showBubbleDown(anchor, mActivity, mSortList, new BubblePopAdapter.OnPopListClickListener() {
//                @Override
//                public void setOnItemClickListener(int i, View view) {
//                    switch (i) {
//                        case 0:
//                            mSortList.get(0).setSelect(true);
//                            break;
//                        default:
//                            break;
//                    }
//
//                    requestDatas();
//                }
//            });
//        }
    }

    @Override
    public void onDownPull() {
        if (isFunction)
            return;
        if (isShow) {
            ((BaseCardDeskActivity) mActivity).showSearchTitle();
        } else {
            ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        }

        ((BaseCardDeskActivity) mActivity).hideSortBt();

        isShow = !isShow;
    }
}
